using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Exercise.Api.Entity;
using Exercise.Api.ParamModel;
using Newtonsoft.Json;
using System;
using Exercise.Api.Repository;

namespace Exercise.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private Api.Db.ExerciseDb db=new Api.Db.ExerciseDb();

        private IRepository<Users>_usersRespository=new EfRepository<Users>();
        //获取所有用户
        [HttpGet]
        public dynamic Get()
        {
            var users =db.Users.ToList();
            return new{
                Code=1000,
                Data=users,
                Msg="获取所有用户成功！"
            };
        }

        //根据ID查找用户
        [HttpGet("{id}")]
        public dynamic GetId(int id)
        {
            //获取在Users中搜索到的第一个符合条件的数据
            var user=db.Users.Where(x=>x.Id == id).FirstOrDefault();
            return new{
                Code=1000,
                Data=user,
                Msg="要查找的用户不存在，请确认后重试！"
            };
        }

        //创建用户
        [HttpPost]
        public string Created(NewUser model)
        {
            JsonSerializerSettings settings=new JsonSerializerSettings();

            settings.ReferenceLoopHandling=ReferenceLoopHandling.Ignore;

            //获取要创建的用户名和密码
            var user = new Users
            {
                UserName = model.UserName,
                PassWord = model.PassWord
            };
            
            //把创建的用户名和密码添加到Users
            db.Users.Add(user);
            db.SaveChanges();
           var res= new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功！"
            };
            return JsonConvert.SerializeObject(res);
        }

        //根据ID修改用户
        [HttpPut("{id}")]
        public dynamic Updated(int id, NewUser model)
        {
            var user=db.Users.Where(x=>x.Id ==id).FirstOrDefault();

            if (user != null)
            {
                user.UserName=model.UserName;
                user.PassWord=model.PassWord;
                user.UpdatedTime=DateTime.Now;
                db.Update(user);
                db.SaveChanges();

                return new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("用户{0}修改成功！", id)
                };
            }else
            {
                return new
                {
                    Code=104,
                    Data="",
                    Msg=string.Format("用户{0}不存在，请确认后重试！",id)
                };
            }
        }

        //根据ID删除用户
        [HttpDelete("{id}")]
        public dynamic Deleted(int id)
        {
            var user=db.Users.Where(x=>x.Id==id).FirstOrDefault();

            if (user != null)
            {
                db.Users.Remove(user);
                db.SaveChanges();
                return new
                {
                    Code=1000,
                    Msg=string.Format("用户{0}删除成功！",id)
                };
            }else
            {
                return new
                {
                    Code=104,
                    Data="",
                    Msg=string.Format("要删除的用户{0}不存在，请确认后重试！",id)
                };
            }
          
        }

        //获取创建的表，并传入数据
        private IEnumerable<Users> GetUsers()
        {
            var users = new List<Users>{
                new Users{
                    Id=1,
                    UserName="admin",
                    PassWord="123"
                },
                new Users{
                    Id=2,
                    UserName="abc",
                    PassWord="123"
                },
                new Users{
                    Id=3,
                    UserName="adf",
                    PassWord="123"
                }
            };
            return users;
        }

    }
}